import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'author.dart';
import 'widget/author_widget.dart';
import '../../shared/shared.dart';
import '../../widgets/widgets.dart';

class RegisterPage extends GetView<AuthorLogic> {
  const RegisterPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    SizeConfig().init(context);
    return Stack(
      children: [
        const Background(imageURL: AssetsURL.backgroundPicture1, opacity: 0.59),
        _buildScaffold(context)
      ],
    );
  }

  SafeArea _buildScaffold(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        resizeToAvoidBottomInset: false,
        backgroundColor: Colors.transparent,
        body: _buildBody(context),
      ),
    );
  }

  Widget _buildBody(BuildContext context) {
    return Form(
      key: controller.registerFormKey,
      child: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            const AuthorBack(showText: true, text: '注册'),
            CommonWidget.rowHeight(height: 120),
            _buildUser(),
            CommonWidget.rowHeight(height: 24),
            _buildPassword(),
            CommonWidget.rowHeight(height: 24),
            _buildConfirm(),
            CommonWidget.rowHeight(height: 160),
            _buildButton(context),
            CommonWidget.rowHeight(height: 100),
            UserAgreement(controller: controller)
          ],
        ),
      ),
    );
  }

  BlindButton _buildButton(BuildContext context) {
    return BlindButton(
        onTap: () => controller.register(context),
        width: 113,
        height: 39,
        text: '确定',
        fontSize: 20,
        color: Colors.transparent);
  }

  InputField _buildConfirm() {
    return InputField(
      hintText: '请确认密码',
      controller: controller.registerConfirmPasswordController,
      keyboardType: TextInputType.emailAddress,
      password: true,
      validator: (value) {
        if (controller.registerPasswordController.text !=
            controller.registerConfirmPasswordController.text) {
          return '两次密码输入不一致。';
        }

        if (value!.isEmpty) {
          return '请输入确认密码';
        }
        return null;
      },
    );
  }

  InputField _buildPassword() {
    return InputField(
      hintText: '请输入密码',
      controller: controller.registerPasswordController,
      keyboardType: TextInputType.emailAddress,
      password: true,
      validator: (value) {
        if (value!.isEmpty) {
          return '请输入密码';
        }

        if (value.length < 6 || value.length > 15) {
          return '密码长度应该 6-15';
        }

        return null;
      },
    );
  }

  InputField _buildUser() {
    return InputField(
        hintText: '请输入用户名',
        controller: controller.registerUserController,
        keyboardType: TextInputType.text);
  }
}
